package com.xing.controller;

import com.xing.entity.Student;
import com.xing.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/student")
public class StudentHander {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findAll")      //Rest风格，查询用get
    public Collection<Student> findAll(){
        return  studentRepository.findAll();
    }

    @GetMapping("/findById/{id}") //Rest风格，查询用get
    public Student findById(@PathVariable("id") long id){
        return studentRepository.findById(id);
    }

    @PostMapping("/save")    //Rest风格，新增用post,响应的是json格式，要用@RequestBody展示
    public void save(@RequestBody Student student){
         studentRepository.saveOrUpdate(student);
    }

    @PutMapping("/update")   //Rest风格，修改用put,响应的是json格式，要用@RequestBody展示
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }

    @DeleteMapping("/deleteById/{id}")  //Rest风格，删除用delete
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }

}

